<?php
/*
   Copyright 2012 BiSe Trojanov

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

 $imgs_ext=array('','gif','jpg','png');
 function ExtToType($ext){
  $real_ext=preg_replace('|^(.*\\.)?([a-z]+)$|','$2',strtolower($ext));
  if ($real_ext=='gif'){return 1;}
  if ($real_ext=='jpg' or $real_ext=='jpeg'){return 2;}
  if ($real_ext=='png'){return 3;}
  return 0;
 }
 /*
 $source = path to source
 $dest   = path to destination
 $width  = new width  or "auto"
 $height = new height or "auto"
 $circum = '' for cut center of image

 image type of new file = extension of new file
 */
 function ImageResize($source,$dest,$width,$height,$circum='',$quality=true){
  global $imgs_ext, $sad_root;

  if ((int)$width<1  and $width !=='auto'){return 1;}
  if ((int)$height<1 and $height!=='auto'){return 1;}
  if ($width=='auto' and $height =='auto'){return 1;}
  if ($circum!=='' and !preg_match('|^[0-9a-f]{6,6}$|i',$circum)){return 1;}

  list($org_width, $org_height, $tt) = getimagesize($source);
  if ($tt<1 or $tt>3){return 2;}

  switch($tt){
   case 1: $img_source =ImageCreateFromGif($source); break;
   case 3: $img_source =ImageCreateFromPng($source); imageSaveAlpha($img_source, true); break;
   case 2: $img_source =ImageCreateFromJpeg($source); break;
   default: return 2; break;
  }

  //Select params
  $sour_x=0;$sour_y=0;$sour_w=$org_width;$sour_h=$org_height;
  $dest_x=0;$dest_y=0;$dest_w=$width;$dest_h=$height;

  if ($width =='auto'){
   $dest_h=$height;$dest_w=(int)($org_width* ($dest_h/$org_height));
  }
  if ($height=='auto'){
   $dest_w=$width; $dest_h=(int)($org_height*($dest_w/$org_width));
  }
  if ((int)$width>0 and (int)$height>0){
   if ($circum==''){
    //cut
    if ($height*$sour_w/$width<=$sour_h){
     $sour_h=floor($height*$org_width/$width);
    }else{
     $sour_w=floor($width*$org_height/$height);
    }
    if ($org_width !==$sour_w){$sour_x=floor($org_width/2 -$sour_w/2);}
    if ($org_height!==$sour_h){$sour_y=floor($org_height/2-$sour_h/2);}
   }else{
    //wings
    return -1;

   }
  }

  //$dest
  $dd  =substr($dest,strlen($sad_root)+1);
  $ar  =explode('/',$dd);$path=$sad_root;unset($ar[count($ar)-1]);
  foreach ($ar as $dir){
   $path.='/'.$dir;
   if (!file_exists($path)){mkdir($path);}
  }
  unset($ar,$path,$dir,$dd);

  //Create new image
  $img_dist=imagecreatetruecolor($dest_w,$dest_h);
  if ($quality){
   imagecopyresampled($img_dist, $img_source, $dest_x, $dest_y, $sour_x, $sour_y, $dest_w, $dest_h, $sour_w, $sour_h);
  }else{ 
   imagecopyresized($img_dist, $img_source, $dest_x, $dest_y, $sour_x, $sour_y, $dest_w, $dest_h, $sour_w, $sour_h);
  }
  
  //Save
  switch(ExtToType($dest)){
   case 1: $img_dest =imagegif($img_dist,$dest);     break;
   case 3: $img_dest =imagepng($img_dist,$dest);     break;
   case 2: $img_dest =imagejpeg($img_dist,$dest,98); break;
   default: return 3; break;
  }// */
  return 0;
//  echo "$sour_x; $sour_y; {$sour_w}x{$sour_h} | $dest_x; $dest_y; {$dest_w}x{$dest_h}";
 }
 
 function HSV2RGB($H,$S,$V){
  $Hi=floor($H/60) % 6;
  $f =$H/60 - floor($H/60);
  $S =$S/100;$V=$V/100;

  $p =$V*(1-$S);
  $q =$V*(1-$f*$S);
  $t =$V*(1-(1-$f)*$S);

  $a=array();
  switch($Hi){
   case 0: $a[0]=$V;$a[1]=$t;$a[2]=$p; break;
   case 1: $a[0]=$q;$a[1]=$V;$a[2]=$p; break;
   case 2: $a[0]=$p;$a[1]=$V;$a[2]=$t; break;
   case 3: $a[0]=$p;$a[1]=$q;$a[2]=$V; break;
   case 4: $a[0]=$t;$a[1]=$p;$a[2]=$V; break;
   case 5: $a[0]=$V;$a[1]=$p;$a[2]=$q; break;
  }
  $a[0]=round($a[0]*255);$a[1]=round($a[1]*255);$a[2]=round($a[2]*255);
  return $a;
 }
 
 function rgb2hsv($r,$g,$b){
  $r=$r/255;$g=$g/255;$b=$b/255;
  $max=max($r,$g,$b);
  $min=min($r,$g,$b);
  if ($max==$min){$h=0;}else{
   $ar =array(
    ($g-$b)/($max-$min)*60+0,
    ($g-$b)/($max-$min)*60+360,
    ($b-$r)/($max-$min)*60+120,
    ($r-$g)/($max-$min)*60+240
   );
   if ($max==$r && $g>=$b){$h=$ar[0];}
   if ($max==$r && $g< $b){$h=$ar[1];}
   if ($max==$g){$h=$ar[2];}
   if ($max==$b){$h=$ar[3];}
  }
  
  if ($max==0){$s=0;}else{$s=1-($min/$max);}
  return array(round($h), round($s*100), round($max*100));
 }
 
 function parseHC($text){
  if (!preg_match('|([0-9a-f]{2,2})([0-9a-f]{2,2})([0-9a-f]{2,2})|i',$text,$a)){
   if (!preg_match('|([0-9a-f])([0-9a-f])([0-9a-f])|i',$text,$a)){return null;}
   $a=array(0, $a[1].$a[1], $a[2].$a[2], $a[3].$a[3]);
  }
  return array(hexdec($a[1]), hexdec($a[2]), hexdec($a[3]));
 }

 function createHC($r,$g,$b){
  $c=($r << 16) | ($g << 8) | $b;
  $s=dechex($c);
  while (strlen($s)<6){$s='0'.$s;}
  return $s;
 } 
 
?>